/**
 * 
 */
package org.vsg.stock.dzh;

import java.io.File;
import java.util.List;
import java.util.Vector;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.vsg.common.configration.PropertiesSupport;
import org.vsg.stock.basic.Stock;
import org.vsg.stock.basic.domain.GroupingStockInfo;
import org.vsg.stock.basic.domain.RealStockInfo;
import org.vsg.stock.basic.domain.StockInfo;
import org.vsg.stock.basic.persistence.StockInfoDao;
import org.vsg.stock.basic.service.StockService;
import org.vsg.stock.extension.service.StockMigrationService;


/**
 * @author vison
 *
 */
public class UpdateStockInfo {

	private final static Logger logger = LoggerFactory.getLogger(UpdateStockInfo.class);	
	
	
	public void exec(ApplicationContext appContext) throws Exception {

		PropertiesSupport proSupport = (PropertiesSupport)appContext.getBean("propertyConfigurer");
		
		String vsatDir = proSupport.getProperties().getProperty("stock.realvsat");
		
		// --- import concept ---
		File vsatD = new File(vsatDir);	
		
		StockService stkService = (StockService)appContext.getBean("stockService");
		
		StockInfoDao stockInfoDao = (StockInfoDao)appContext.getBean("stockInfoDao");
		
		StockMigrationService stockMigrationService = (StockMigrationService) appContext.getBean("stockMigrationService");
		
		List<Stock> allStk = stkService.getAvaiableStocks();
		// --- clean old data ---
		stockInfoDao.cleanInfoPartMap();
		
		stockInfoDao.cleanAll();
		
		List<StockInfo> entities = new Vector<StockInfo>();
		
		File stkFile = null;
		
		for (Stock stk : allStk) {
			stkFile = new File(new File(vsatDir,stk.getType()) , stk.getCode()+".txt");
			
			StockInfo stkInfo = null;
			if (stk.getKind().equals("G")) {
				GroupingStockInfo gsi = new GroupingStockInfo();
				stkInfo = gsi;
			} 
			
			else if (stk.getKind().equals("R")){
				RealStockInfo rsi = new RealStockInfo();
				stkInfo = rsi;
				
			}

			stkInfo.setName( stk.getName() );
			stkInfo.setCode( stk.getType()+stk.getCode() );

			stkInfo = stockMigrationService.updateStockInfoFromVsatData(stkInfo, stkFile);
			entities.add( stkInfo );
		}
		
		stockInfoDao.saveAll(entities);
		
		
		// --- update stock map ----
		List<String> groupingStkList = stockInfoDao.findAllGroupingCode();
		
		File baseDir = null;
		List<String> mapInfoCodes = null;
		for (String code : groupingStkList) {
			if (code.startsWith("SH")) {
				baseDir = new File(vsatD, "SHbase");
			} else if (code.startsWith("SZ") ) {
				baseDir = new File(vsatD, "SZbase");
			}
			
			// --- get the file name filter ---
			String fileName = code.substring(2)+".002";
			File partFile = new File(baseDir,fileName);
			
			List<String> stockMapList = FileUtils.readLines(partFile, "GBK");
			String firstList = stockMapList.get(0);
			if (firstList.indexOf("◆  成分股  ◆") == -1) {
				continue;
			}
			
			mapInfoCodes = new Vector<String>();
			String line = null;
			for (int i = 4 ; i < stockMapList.size() ; i++) {
				line = stockMapList.get(i);
				String[] contentData = line.split("     ");
				String mapCode = contentData[1].trim(); 
				if ( mapCode.startsWith("60") ) {
					mapInfoCodes.add("SH"+ mapCode);
				} else if ( mapCode.startsWith("00") ) {
					mapInfoCodes.add("SZ"+ mapCode);
				}
			}
			stockInfoDao.updateStockInfoPartMap(code, mapInfoCodes);
		}
		



	}
	
	

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ClassLoader clsLoader = UpdateStockInfo.class.getClassLoader();
		// --- run the main page ---
		// build the read the path , dzh's home path
		String[] configContextFiles = {
				"classpath:/spring/configurationContext.xml",
				"classpath*:/spring/applicationContext.xml",
				"classpath*:/spring/dataAccessContext.xml"
		};
		
		ApplicationContext ctx = new ClassPathXmlApplicationContext(configContextFiles,true);
		
	
		UpdateStockInfo mainProg = new UpdateStockInfo();
		
		try {
			mainProg.exec(ctx);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		logger.info("program run ... Ok!");
		



	}

}
